// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.ai.states;

import java.util.HashMap;
import zombie.ai.State;
import zombie.audio.parameters.ParameterZombieState;
import zombie.characters.IsoGameCharacter;
import zombie.characters.IsoZombie;
import zombie.core.Rand;
import zombie.core.skinnedmodel.advancedanimation.AnimEvent;
import zombie.iso.IsoDirections;
import zombie.iso.objects.IsoZombieGiblets;

public final class ZombieHitReactionState extends State {
    private static final ZombieHitReactionState _instance = new ZombieHitReactionState();
    private static final int TURN_TO_PLAYER = 1;
    private static final int HIT_REACTION_TIMER = 2;

    public static ZombieHitReactionState instance() {
        return _instance;
    }

    @Override
    public void enter(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        isoZombie.collideWhileHit = true;
        HashMap hashMap = owner.getStateMachineParams(this);
        hashMap.put(1, Boolean.FALSE);
        hashMap.put(2, 0.0F);
        owner.clearVariable("onknees");
        if (isoZombie.isSitAgainstWall()) {
            owner.setHitReaction(null);
        }
    }

    @Override
    public void execute(IsoGameCharacter owner) {
        HashMap hashMap = owner.getStateMachineParams(this);
        owner.setOnFloor(((IsoZombie)owner).isKnockedDown());
        hashMap.put(2, (Float)hashMap.get(2) + zombie.GameTime.getInstance().getMultiplier());
        if (hashMap.get(1) == Boolean.TRUE) {
            if (!owner.isHitFromBehind()) {
                owner.setDir(IsoDirections.reverse(IsoDirections.fromAngle(owner.getHitDir())));
            } else {
                owner.setDir(IsoDirections.fromAngle(owner.getHitDir()));
            }
        } else if (owner.hasAnimationPlayer()) {
            owner.getAnimationPlayer().setTargetToAngle();
        }
    }

    @Override
    public void exit(IsoGameCharacter owner) {
        IsoZombie isoZombie = (IsoZombie)owner;
        isoZombie.collideWhileHit = true;
        if (isoZombie.target != null) {
            isoZombie.AllowRepathDelay = 0.0F;
            isoZombie.spotted(isoZombie.target, true);
        }

        isoZombie.setStaggerBack(false);
        isoZombie.setHitReaction("");
        isoZombie.setEatBodyTarget(null, false);
        isoZombie.setSitAgainstWall(false);
        isoZombie.setShootable(true);
    }

    @Override
    public void animEvent(IsoGameCharacter owner, AnimEvent event) {
        HashMap hashMap = owner.getStateMachineParams(this);
        IsoZombie isoZombie = (IsoZombie)owner;
        if (event.m_EventName.equalsIgnoreCase("DoDeath") && Boolean.parseBoolean(event.m_ParameterValue) && owner.isAlive()) {
            owner.Kill(owner.getAttackedBy());
            if (isoZombie.upKillCount && owner.getAttackedBy() != null) {
                owner.getAttackedBy().setZombieKills(owner.getAttackedBy().getZombieKills() + 1);
            }
        }

        if (event.m_EventName.equalsIgnoreCase("PlayDeathSound")) {
            owner.setDoDeathSound(false);
            owner.playDeadSound();
        }

        if (event.m_EventName.equalsIgnoreCase("FallOnFront")) {
            owner.setFallOnFront(Boolean.parseBoolean(event.m_ParameterValue));
        }

        if (event.m_EventName.equalsIgnoreCase("ActiveAnimFinishing")) {
        }

        if (event.m_EventName.equalsIgnoreCase("Collide") && ((IsoZombie)owner).speedType == 1) {
            ((IsoZombie)owner).collideWhileHit = false;
        }

        if (event.m_EventName.equalsIgnoreCase("ZombieTurnToPlayer")) {
            boolean boolean0 = Boolean.parseBoolean(event.m_ParameterValue);
            hashMap.put(1, boolean0 ? Boolean.TRUE : Boolean.FALSE);
        }

        if (event.m_EventName.equalsIgnoreCase("CancelKnockDown")) {
            boolean boolean1 = Boolean.parseBoolean(event.m_ParameterValue);
            if (boolean1) {
                ((IsoZombie)owner).setKnockedDown(false);
            }
        }

        if (event.m_EventName.equalsIgnoreCase("KnockDown")) {
            owner.setOnFloor(true);
            ((IsoZombie)owner).setKnockedDown(true);
        }

        if (event.m_EventName.equalsIgnoreCase("SplatBlood")) {
            isoZombie.addBlood(null, true, false, false);
            isoZombie.addBlood(null, true, false, false);
            isoZombie.addBlood(null, true, false, false);
            isoZombie.playBloodSplatterSound();

            for (int _int = 0; _int < 10; _int++) {
                isoZombie.getCurrentSquare()
                    .getChunk()
                    .addBloodSplat(isoZombie.x + Rand.Next(-0.5F, 0.5F), isoZombie.y + Rand.Next(-0.5F, 0.5F), isoZombie.z, Rand.Next(8));
                if (Rand.Next(5) == 0) {
                    new IsoZombieGiblets(
                        IsoZombieGiblets.GibletType.B,
                        isoZombie.getCell(),
                        isoZombie.getX(),
                        isoZombie.getY(),
                        isoZombie.getZ() + 0.3F,
                        Rand.Next(-0.2F, 0.2F) * 1.5F,
                        Rand.Next(-0.2F, 0.2F) * 1.5F
                    );
                } else {
                    new IsoZombieGiblets(
                        IsoZombieGiblets.GibletType.A,
                        isoZombie.getCell(),
                        isoZombie.getX(),
                        isoZombie.getY(),
                        isoZombie.getZ() + 0.3F,
                        Rand.Next(-0.2F, 0.2F) * 1.5F,
                        Rand.Next(-0.2F, 0.2F) * 1.5F
                    );
                }
            }
        }

        if (event.m_EventName.equalsIgnoreCase("SetState") && !isoZombie.isDead()) {
            if (isoZombie.getAttackedBy() != null && isoZombie.getAttackedBy().getVehicle() != null && "Floor".equals(isoZombie.getHitReaction())) {
                isoZombie.parameterZombieState.setState(ParameterZombieState.State.RunOver);
                return;
            }

            isoZombie.parameterZombieState.setState(ParameterZombieState.State.Hit);
        }
    }
}
